package ipad.asia.controller;

import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.utils.FtpUtil;
import ipad.asia.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@RestController
public class RegisterController {
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private MD5Util md5Util;
    @RequestMapping("/register")
    public ModelAndView register(@RequestParam(value = "user", required = false) String user,
                                    @RequestParam(value = "password", required = false) String password,
                                    @RequestParam(value = "email", required = false) String email,
                                    @RequestParam(value = "phone", required = false) String phone,
                                 @RequestParam(value = "shopName",required = false) String shopName,
                                 @RequestParam(value = "credentials",required = false) MultipartFile credentials,
                                 @RequestParam(value = "address",required = false) String address
                                 ) throws IOException {
        System.out.println("测试注册！");
        System.out.println(user+shopName+password);
        if(user==null&&shopName==null&&password==null){
            return new ModelAndView("register",
                    "message","用户名，密码，店铺名不能为空");
        }else {

            if (enterpriseFeign.register(user,MD5Util.inputPassToFormPass(password) , email,phone,shopName,credentials.getOriginalFilename(),address) == true) {
                boolean b = FtpUtil.uploadFile(credentials.getOriginalFilename(), credentials.getInputStream());
                System.out.println("测试注册！！"+b);
                return new ModelAndView("login", "message", "注册成功，请登录");
        }else {
            return new ModelAndView("register", "message", "注册失败，用户已存在或用户名，密码，店铺名不能为空");
        }
        }
    }
}
